﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SPSearchHelper.cs" company="Universal Music Group">
//   All rights reserved
// </copyright>
// <summary>
//   Provides common utilities for interacting with SharePoint search infrastructure.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LifeInSharePoint.Common
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Office.Server.Search.Administration;
    using Microsoft.SharePoint;

    /// <summary>
    /// Provides common utilities for interacting with SharePoint search infrastructure.
    /// </summary>
    public static class SPSearchHelper
    {
        #region Public Methods

        /// <summary>
        /// Gets a list of display groups for a particular site.
        /// </summary>
        /// <param name="siteUrl">
        /// The site Url
        /// </param>
        /// <returns>
        /// A string list of the display group names.
        /// </returns>
        public static List<string> GetDisplayGroupsForSite(string siteUrl)
        {
            List<string> returnValue = new List<string>();
            using (SPSite currentSiteCollection = new SPSite(siteUrl))
            {
                // get a search context and get access to the scope manager
                SearchContext searchContext = SearchContext.GetContext(currentSiteCollection);
                Scopes scopes = new Scopes(searchContext);

                // get all scope display groups
                ScopeDisplayGroupCollection scopeDisplayGroups = scopes.AllDisplayGroups;

                // foreach (ScopeDisplayGroup scd in scopeDisplayGroups)
                foreach (ScopeDisplayGroup scd in scopes.GetDisplayGroupsForSite(new Uri(siteUrl)))
                {
                    returnValue.Add(scd.Name);
                }
            }

            return returnValue;
        }

        /// <summary>
        /// Gets a list of search scopes for a specific display group in a particular site.
        /// </summary>
        /// <param name="siteUrl">
        /// The site Url
        /// </param>
        /// <param name="displayGroup">
        /// The display group name
        /// </param>
        /// <param name="defaultScope">
        /// The default Scope.
        /// </param>
        /// <returns>
        /// A string list of the search scope names.
        /// </returns>
        public static List<string> GetScopesForSiteDisplayGroup(string siteUrl, string displayGroup, out string defaultScope)
        {
            List<string> returnValue = new List<string>();
            using (SPSite currentSiteCollection = new SPSite(siteUrl))
            {
                // get a search context and get access to the scope manager
                SearchContext searchContext = SearchContext.GetContext(currentSiteCollection);
                Scopes scopes = new Scopes(searchContext);

                // get the scope display group
                ScopeDisplayGroup sdg = scopes.GetDisplayGroup(new Uri(siteUrl), displayGroup);

                // Get all scopes
                for (int i = 0; i < sdg.Count; i++)
                {
                    returnValue.Add(sdg[i].Name);
                }

                // Get the default scope
                defaultScope = sdg.Default.Name;
            }

            return returnValue;
        }

        #endregion
    }
}